# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/7/8 11:26
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : inject_tbl_structure.py
# @Version: planner_builder 1.0

import ctypes

from tsn_builder_struct.table_length import INJECT_TBL_LEN

c_uint32 = ctypes.c_uint32


class InjectEntry_bits(ctypes.BigEndianStructure):
    _fields_ = [
        # entry1
        ('valid', c_uint32, 1),
        ('reserved', c_uint32, 12),
        ('dbuf_id', c_uint32, 9),
        ('time_slot', c_uint32, 10),
    ]


class InjectEntry_bytes(ctypes.BigEndianStructure):
    _fields_ = [
        ('entry1', c_uint32, 32),
    ]


class InjectEntry_union(ctypes.Union):
    _fields_ = [
        ('bit', InjectEntry_bits),
        ('byte', InjectEntry_bytes)
    ]


class InjectTblEachNode:
    def __init__(self,
                 node_id: int):
        self.node_id = node_id
        self.tbl_len = INJECT_TBL_LEN
        self.tbl_each_node = []

    def append_entry(self,
                     entry: InjectEntry_union):
        if len(self.tbl_each_node) <= self.tbl_len:
            self.tbl_each_node.append(entry)
        else:
            print('ERROR - inject tbl exceeds %d at node %d' % (self.tbl_len, self.node_id))
            exit(0)


class InjectTbl:
    def __init__(self):
        self.tbl_set = []

    def append_tbl(self,
                   tbl: InjectTblEachNode):
        self.tbl_set.append(tbl)
